package com.jcb.countfrench;


import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;

import com.jcb.countfrench.dao.CountFrenchDAO;
import com.jcb.countfrench.model.Quizz;
import com.jcb.countfrench.model.User;
import com.jcb.countfrench.presenter.CountPresenter;
import com.jcb.countfrench.presenter.QuizzChoicePresenter;
import com.jcb.countfrench.presenter.QuizzPresenter;
import com.jcb.countfrench.view.AboutView;
import com.jcb.countfrench.view.AdminView;
import com.jcb.countfrench.view.InitialView;
import com.jcb.countfrench.view.LearnView;
import com.jcb.countfrench.view.QuizzChoiceView;
import com.jcb.countfrench.view.QuizzView;

public class MainActivity extends Activity {
	private User user = new User("jcb");
	public static MediaPlayer mediaPlayer ;
	private Button mquizzChineseCharacters, mlearnLevel1, buttonQuizzPinyin ;
	public static CountFrenchDAO dao ;
	private CountPresenter countPresenter ;
	private QuizzPresenter quizzPresenter ;
	private QuizzChoicePresenter quizzChoicePresenter ;
	private LearnView learnView ;
	private InitialView initialView ;
	private AboutView aboutView ;
	private QuizzChoiceView quizzChoiceView ;
	private QuizzView quizzView ;
	
	
	public QuizzChoicePresenter getQuizzChoicePresenter() {
		return quizzChoicePresenter;
	}

	public QuizzPresenter getQuizzPresenter() {
		return quizzPresenter;
	}

	public void setQuizzPresenter(QuizzPresenter quizzPresenter) {
		this.quizzPresenter = quizzPresenter;
	}

	public CountPresenter getCountPresenter() {
		return countPresenter;
	}

	public void setCountPresenter(CountPresenter countPresenter) {
		this.countPresenter = countPresenter;
	}


	public LearnView getLearnView() {
		return learnView;
	}

	public void displayLearnView() {
		setContentView(learnView);
		learnView.initialDisplay();
	}

	public void displayQuizzChoiceView() {
		setContentView(quizzChoiceView);
	}
	
	public void displayQuizzView(Quizz quizzCourant) {
		setContentView(quizzView);
		quizzView.initialDisplay(quizzCourant);
	}
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.splash);
	    dao = new CountFrenchDAO(this);
	    mediaPlayer = MediaPlayer.create(this, R.raw.ma1);
	    
	    //presenter
	    countPresenter = new CountPresenter(user ,dao,this, this);	
	    quizzPresenter = new QuizzPresenter (user ,dao,this, this);
	    quizzChoicePresenter = new QuizzChoicePresenter(user ,dao,this, this);	    
	    
	    //vues
	    initialView 		= new InitialView(this, this, user, countPresenter);
	    learnView 			= new LearnView(this,  user) ;
	    quizzView 			= new QuizzView(this, this, user, countPresenter);
	    quizzChoiceView  	= new QuizzChoiceView(this, this, user, quizzChoicePresenter);
	    aboutView			= new AboutView (this);
	    
	    learnView.setCountPresenter(countPresenter);
	    learnView.setMediaPlayer(mediaPlayer);	    
	    quizzView.setQuizzPresenter(quizzPresenter) ;
	    quizzView.setMediaPlayer(mediaPlayer);

	    new Timer().schedule(new DisplayTask(), 800);

	}

    public void launchAdmin() {
    	AdminView adminView  = new AdminView(this, dao);
    	setContentView(adminView);
    } 
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.homeMenu:
            	//initInitialMenus();
            	//setContentView(R.layout.initial_menu);
            	Log.i("WXW", "home" );
            	setContentView(initialView);
                return true;
            case R.id.aboutMenu:
            	setContentView(aboutView);     
                return true;
            case R.id.settingMenu:    
            	//initInitialMenus();
            case R.id.adminMenu:    
            	launchAdmin();                
            default:
                return super.onOptionsItemSelected(item);
        }
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.i("", "onCreateOptionsMenu" + Thread.currentThread().getName());
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menus, menu);
        return true;
    }
	
	private class DisplayTask extends TimerTask {
		public DisplayTask() {
			super();
		}

		@Override
		public void run() {
			MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {	                	
                	setContentView(initialView);
                }
            });			
		}
	    	
	}

	
}
